<template>
  <section class="imgs-list">
    <div class="left" @click="setActiveImg(currentIndex - 1)">
      <el-icon>
        <arrow-left></arrow-left>
      </el-icon>
    </div>
    <div class="right" @click="setActiveImg(currentIndex + 1)">
      <el-icon>
        <arrow-right></arrow-right>
      </el-icon>
    </div>
    <header>
      <div ref="innerRef" class="inner">
        <img
          v-for="(img, index) of imgs"
          :key="img"
          :src="img"
          :style="itemStyle(index)"
        />
      </div>
    </header>
    <div class="slider">
      <item
        v-for="(item, index) of imgs"
        :key="item"
        :img="item"
        :active="index === currentIndex"
        @click="setActiveImg(index)"
      ></item>
    </div>
  </section>
</template>

<script lang="ts" setup>
import Item from './item.vue';
import { ArrowLeft, ArrowRight } from '@element-plus/icons-vue';
import { CSSProperties, PropType, ref } from 'vue';

const props = defineProps({
  imgs: {
    type: Array as PropType<string[]>,
    required: true,
  },
});
const innerRef = ref<HTMLElement>();
const currentIndex = ref(0);

//funcs
const itemStyle = index => {
  return {
    transform: `translateX(${(index - currentIndex.value) * 455}px)`,
  } as CSSProperties;
};

const processIndex = (index: number) => {
  if (index > props.imgs.length - 1) {
    return 0;
  } else if (index < 0) {
    return props.imgs.length - 1;
  } else {
    return index;
  }
};

const setActiveImg = (index: number) => {
  currentIndex.value = processIndex(index);
};
</script>

<style lang="scss" scoped>
.imgs-list {
  user-select: none;
  position: relative;
  width: max-content;

  .left,
  .right {
    top: 50%;
    width: 30px;
    height: 30px;
    z-index: 999;
    display: flex;
    cursor: pointer;
    align-items: center;
    border-radius: 50%;
    position: absolute;
    justify-content: center;
    transform: translateY(-50%);
    border: 1px solid var(--grey-color12);
    background-color: var(--white-color);

    &:hover {
      background-color: var(--grey-color12);
    }
  }

  .left {
    left: 5px;
  }

  .right {
    right: -5px;
  }

  .slider {
    display: flex;
    justify-content: center;
  }

  header {
    width: 500px;
    height: 500px;
    display: flex;
    padding: 0 30px;
    overflow: hidden;
    position: relative;
    justify-content: center;

    .inner {
      width: 400px;
      display: flex;
      position: relative;

      img {
        left: 0;
        width: 100%;
        height: 100%;
        position: absolute;
        object-fit: cover;
        transition: all 0.4s;
      }
    }
  }
}
</style>
